<?php

/**
 * Created by PhpStorm.
 * User: dapeng
 * Date: 2018/9/28
 * Time: 上午9:29
 */
//include "db.php";
class MyPDO
{
    //成员变量
    private $mysql;
    private $db = "test123";
    private $host = "127.0.0.1";
    private $user = "root";
    private $pass = "123456";
    private $charts = "utf8";//utf8 | gb2312 | gbk


    //构造方法 连接数据库
    function __construct()
    {
        try{
            //1.连接数据库
            $this->mysql = new PDO("mysql:dbname={$this->db};host=$this->host","$this->user","$this->pass");
            $this->mysql->exec("SET NAMES '$this->charts';");
        }catch (Exception $e){
            echo "连接失败:";
            var_dump($e);
            die();
        }
    }

    function insert($sql){
        return $this->mysql->exec($sql);

    }
    function delete($sql){
        return $this->mysql->exec($sql);
    }

    function update($sql){
        return $this->mysql->exec($sql);
    }

    function select($sql){

        $res = $this->mysql->query($sql,PDO::FETCH_ASSOC);
        //$res 它是一个PDO对象
        $arr = [];//希望存储的是普通的关联数组
        foreach ($res as $row){
            $arr[] = $row;
//            array_push($arr,$row);
        }
        return $arr;
    }





}